/*-------------------------------------------------------------------------
 *
 * dbasedesc.c
 *	  命令/dbcommands.c 的 rmgr 描述例程
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/backend/access/rmgrdesc/dbasedesc.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "commands/dbcommands_xlog.h"
#include "lib/stringinfo.h"


void dbase_desc(StringInfo fc_buf, XLogReaderState *fc_record)
{
	char	   *fc_rec = XLogRecGetData(fc_record);
	uint8		fc_info = XLogRecGetInfo(fc_record) & ~XLR_INFO_MASK;

	if (fc_info == XLOG_DBASE_CREATE_FILE_COPY)
	{
		xl_dbase_create_file_copy_rec *fc_xlrec =
		(xl_dbase_create_file_copy_rec *) fc_rec;

		appendStringInfo(fc_buf, "copy dir %u/%u to %u/%u",
						 fc_xlrec->src_tablespace_id, fc_xlrec->src_db_id,
						 fc_xlrec->tablespace_id, fc_xlrec->db_id);
	}
	else if (fc_info == XLOG_DBASE_CREATE_WAL_LOG)
	{
		xl_dbase_create_wal_log_rec *fc_xlrec =
		(xl_dbase_create_wal_log_rec *) fc_rec;

		appendStringInfo(fc_buf, "create dir %u/%u",
						 fc_xlrec->tablespace_id, fc_xlrec->db_id);
	}
	else if (fc_info == XLOG_DBASE_DROP)
	{
		xl_dbase_drop_rec *fc_xlrec = (xl_dbase_drop_rec *) fc_rec;
		int			fc_i;

		appendStringInfoString(fc_buf, "dir");
		for (fc_i = 0; fc_i < fc_xlrec->ntablespaces; fc_i++)
			appendStringInfo(fc_buf, " %u/%u",
							 fc_xlrec->tablespace_ids[fc_i], fc_xlrec->db_id);
	}
}

const char * dbase_identify(uint8 fc_info)
{
	const char *fc_id = NULL;

	switch (fc_info & ~XLR_INFO_MASK)
	{
		case XLOG_DBASE_CREATE_FILE_COPY:
			fc_id = "CREATE_FILE_COPY";
			break;
		case XLOG_DBASE_CREATE_WAL_LOG:
			fc_id = "CREATE_WAL_LOG";
			break;
		case XLOG_DBASE_DROP:
			fc_id = "DROP";
			break;
	}

	return fc_id;
}
